package com.softer.chengxin.score.service.impl;

import com.softer.chengxin.common.domin.Common;
import com.softer.chengxin.common.domin.JsonResult;
import com.softer.chengxin.common.domin.ManagerPageResult;
import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.common.util.DateFormatUtil;
import com.softer.chengxin.scheduled.dao.ScheduledTaskDao;
import com.softer.chengxin.score.dao.ScoreManagerDao;
import com.softer.chengxin.score.entity.ScoreRecord;
import com.softer.chengxin.score.service.ScoreManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2018/10/12
 */
@Service
public class ScoreManagerServiceImpl implements ScoreManagerService {
    @Autowired
    private ScoreManagerDao scoreManagerDao;
    @Autowired
    private SDManagerServiceImpl sdManagerService;
    @Autowired
    private ScheduledTaskDao scheduledTaskDao;

    /**
     * 获取企业信用得分
     *
     * @param id 企业ID
     * @return 得分情况
     */
    @Override
    public ScoreRecord getScore(Integer id) {
        //获取最近设置的定时任务
        String time = scheduledTaskDao.getTime();
        //获取主管部门定义的年份
        String year = DateFormatUtil.getYear(time);
        return scoreManagerDao.getScore(id, year);
    }

    /**
     * 获取企业信用等级列表
     *
     * @param page        页面实体
     * @param scoreRecord 查询实体
     * @return 信用等级集合
     */
    @Override
    public ManagerPageResult getScoreList(Page page, ScoreRecord scoreRecord) {
        ManagerPageResult mr = new ManagerPageResult();
        JsonResult jr = new JsonResult();
        int num = scoreManagerDao.getScoreRecordNum(scoreRecord);
        if (num > Integer.parseInt(Common.ZERO)) {
            List<ScoreRecord> list = scoreManagerDao.getScoreRecordList(page, scoreRecord);
            //计算各项总得分
            sdManagerService.getAllScore(list);
            return setResult(mr, jr, num, list);
        }
        return setResult(mr, jr, Integer.parseInt(Common.ZERO), new ArrayList());
    }

    /**
     * 设置返回参数
     *
     * @param mpr  ManagerPageResult
     * @param jr   JsonResult
     * @param num  数量
     * @param list 集合列表
     */
    private ManagerPageResult setResult(ManagerPageResult mpr, JsonResult jr, int num, List list) {
        mpr.setCount(num);
        jr.setDs(list);
        mpr.setJson(jr);
        return mpr;
    }
}
